package com.goldcn.classify.service;

import com.goldcn.classify.dto.ClazzObjDto;
import com.goldcn.classify.model.Classify;
import com.goldcn.classify.model.ClassifyToAttrDef;
import com.goldcn.classify.model.Style;
import com.goldcn.common.consts.RestConst;
import com.goldcn.common.exception.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/6/13.
 */
@Service("classifyAttrService")
@Transactional
public class ClassifyAttrService {

    @Autowired
    private ClassifyAttrDefService classifyAttrDefService;
    @Autowired
    private ClassifyAttrValService classifyAttrValService;
    @Autowired
    private ClassifyToAttrDefService classifyToAttrDefService;
    @Autowired
    private ClassifyService classifyService;
    @Autowired
    private StyleService styleService;
    @Autowired
    private PlatformService platformService;

    @Transactional
    public Boolean addAttributes(ClazzObjDto list){
        //判断是添加还是修改
        if(list.getClassifyAttrDef().getCfadID()==""){
            //add
            String cfadID=classifyAttrDefService.insertSelective(list.getClassifyAttrDef());

            for(int i=0;i<list.getClassifyAttrVals().size();i++){
                list.getClassifyAttrVals().get(i).setCfadID(cfadID);
                list.getClassifyAttrVals().get(i).setClazzAttrValCode(i+1+"");
                classifyAttrValService.insertSelective(list.getClassifyAttrVals().get(i));
            }

            //关联属性 delete - add
            platformService.deleteByResID(cfadID);
            for(Integer platform:list.getPlatforms()){
                platformService.insert(cfadID,platform,
                        list.getClassifyAttrDef().getClazzAttrName());
            }


        }else{
            //update
            classifyAttrDefService.selectByPrimaryKey(list.getClassifyAttrDef().getCfadID());

            //关联属性 delete - add
            platformService.deleteByResID(list.getClassifyAttrDef().getCfadID());
            for(Integer platform:list.getPlatforms()){
                platformService.insert(list.getClassifyAttrDef().getCfadID(),platform,
                        list.getClassifyAttrDef().getClazzAttrName());
            }

            if(classifyAttrDefService.updateByPrimaryKeySelective(list.getClassifyAttrDef())<1){

            };
            for(int i=0;i<list.getClassifyAttrVals().size();i++){
                //判断选项是添加还是修改
                if(list.getClassifyAttrVals().get(i).getCavID().equals("")||
                        list.getClassifyAttrVals().get(i).getCavID()==null){
                    //add
                    classifyAttrValService.insertSelective(list.getClassifyAttrVals().get(i));
                }else{
                    //update
                    classifyAttrValService.updateByPrimaryKeySelective(list.getClassifyAttrVals().get(i));
                }
            }
        }
        return true;
    }


    public List<Object> selectClazzObjList(Map<String,String> list){
        List<Object> objlist=new ArrayList<Object>();
        Map<String,String> parm=new HashMap<String,String>();
        parm.put("cfadID",list.get("cfadID"));
        List<ClassifyToAttrDef> ctdlist=new ArrayList<ClassifyToAttrDef>();
        ctdlist= classifyToAttrDefService.selectByClazzAndAttr(parm);
        try{
            switch (Integer.parseInt(list.get("objType"))){
                case 0:
                    for(int i=0;i<ctdlist.size();i++){
                        Classify classify=new Classify();
                        classify=classifyService.checkByPrimaryKey(ctdlist.get(i).getClazzObjID());
                        if(null!=classify){
                            objlist.add(classify);
                        }
                    }
                    break;
                case 1:
                    for(int i=0;i<ctdlist.size();i++){
                        Style style=new Style();
                        style=styleService.detail(ctdlist.get(i).getClazzObjID());
                        if(null!=style){
                            objlist.add(style);
                        }
                    }
                    break;
                case 2:
                    Map<String,Object> objectMap=new HashMap<String,Object>();
                    List<Classify> classifies=new ArrayList<Classify>();
                    List<Style> styles=new ArrayList<Style>();
                    for(int i=0;i<ctdlist.size();i++){
                        Classify classify=new Classify();
                        classify=classifyService.checkByPrimaryKey(ctdlist.get(i).getClazzObjID());
                        if(null!=classify){
                            classifies.add(classify);
                        }else{
                            styles.add(styleService.detail(ctdlist.get(i).getClazzObjID()));
                        }
                    }
                    objectMap.put("classifies",classifies);
                    objectMap.put("styles",styles);
                    objlist.add(objectMap);
                    break;
            }
        }catch (Exception e){
            throw new ServiceException("请求参数错误", RestConst.ErrorCode.DATABASE_ERROR);
        }
        return objlist;
    }

    /**
     *删除多条品类或款式与属性关联
     * @param list
     * @return
     */
    @Transactional
    public boolean deleteObjToAttrList(List<Map<String,String>> list){
        for(Map<String,String> map:list){
            classifyToAttrDefService.deleteByPrimaryKey(
                    classifyToAttrDefService.selectByClazzAndAttr(map).get(0).getCtaID());
        }
        return true;
    }
}
